package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Movie;
import com.beans.Person;
import com.bussiness.MovieBussinessProxy;
import com.bussiness.PersonBussinessProxy;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;
import com.exceptions.DuplicateUserIdException;
import com.exceptions.MovieAlreadyIssuedException;
import com.exceptions.MovieUnavailableException;
import com.exceptions.UserNotEligibleException;

/**
 * Servlet implementation class PaymentServlet
 */
public class PaymentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PersonBussinessProxy personBI = new PersonBussinessProxy();
	MovieBussinessProxy movieBI = new MovieBussinessProxy(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PaymentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
		RequestDispatcher dispatcher;
		Movie[] billMovies;
		Person person = null;
		HttpSession session = request.getSession(true);
		String click = request.getParameter("submit");
		long userid = (Long) session.getAttribute("MembershipId");
		personBI.setEndpoint(ServiceEndPoints.PersonEndPoint);
		//person = personBI.getPersonById(userid);
		if(click.equals("Make Payment")){
			
			int card_type=Integer.parseInt(request.getParameter("choice"));
			String f =request.getParameter("first");
			String s =request.getParameter("second");
			String t =request.getParameter("third");
			String ft =request.getParameter("fourth");
			System.out.println("type :"+card_type+"Card Number:"+f+s+t+ft);
			String card_number = f+s+t+ft;
			String status = personBI.cardValidate(card_number, card_type);
			
				if(status.equals("success")){
					movieBI.setEndpoint(ServiceEndPoints.MovieEndPoint);
					billMovies = (Movie[])session.getAttribute("billMovies");
					for(int i=0;i<billMovies.length;i++){
						//try{
						movieBI.issueMovie(billMovies[i].getMovieId(), userid);
						//}catch(MovieUnavailableException ex)
//						{
//							request.setAttribute("ServerException", "Sorry.. Movie is not available to rent");
//							session.setAttribute("ServerException", "Sorry.. Movie is not available to rent");
//							dispatcher = getServletContext().getRequestDispatcher("/cartDetails.jsp");
//							dispatcher.forward(request, response);
//						}
					}
					dispatcher = getServletContext().getRequestDispatcher("/success.jsp");
					dispatcher.forward(request, response);
				}
				else{
					request.setAttribute("ServerMessage", "The Credit Card Number you entered is Incorrect"); 
					dispatcher = getServletContext().getRequestDispatcher("/card.jsp");
					dispatcher.forward(request, response);
				}
		}
		
		}catch(MovieUnavailableException m){
			request.setAttribute("ServerMessage", "Movie is not available");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/cartDetails.jsp");
			dispatcher.forward(request, response);
		}
		catch(MovieAlreadyIssuedException ml){
			request.setAttribute("ServerMessage", "Movie is already issued to you");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/cartDetails.jsp");
			dispatcher.forward(request, response);
		}
		catch(UserNotEligibleException u){
			request.setAttribute("ServerMessage", "Your membership type does not allow to take this number of movies");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/cartDetails.jsp");
			dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			request.setAttribute("ServerMessage", UIMessages.SERVICE_UNAVAILABLE);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/card.jsp");
			dispatcher.forward(request, response);
		}
	}

}
